<?php
include("../conn.php");
/*获取商户首单返券*/

class getFirCoupon {

	/**
	 * [获取首单返券]
	 * @Author   Axing
	 * @DateTime 2017-07-08
	 * @param    [type] $uid 		商户id
	 * @param    [type]	$cateid 	类别id
	 * @return   [type] $data 		首单返券数据
	 * @return   [type] $recording 	首单返券使用记录
	 */
	public function getCoupon($uid, $goodsid) {
		$time = date('Y-m-d H:i:s');
		if($goodsid) {
			$r = 0;
			$sql = "SELECT DISTINCT xilie FROM goods WHERE goodsid IN (" . $goodsid . ")";
			$res = mysql_query($sql);

			while($row = mysql_fetch_array($res)) {
				echo $row['xile'];
				$cateid .= $row['xilie'] . ',';
				$r++;
			}

			$cateid = rtrim($cateid,',');

			$sql = "SELECT a.id, a.catename, a.cateid ,b.* FROM orders_first AS a LEFT JOIN orders_users_first AS b ON a.id=b.firstid WHERE b.uid=" . $uid . " AND a.cateid IN (0,12," . $cateid . ") AND b.lastTime >= " . "'" . $time . "'";
			
			$res = mysql_query($sql);

		} else {
			$sql = "SELECT a.id, a.catename, a.cateid ,b.* FROM orders_first AS a LEFT JOIN orders_users_first AS b ON a.id=b.firstid WHERE b.uid=" . $uid . " AND b.lastTime >= " . "'" . $time . "'";
			$res = mysql_query($sql);
		}

		if($res) {
			$i = 0;
			$j = 0;
			while ($row = mysql_fetch_array($res)) {
				$data[$i]['id']             =	$row['id'];
				$data[$i]['catename']       =	$row['catename'];
				$data[$i]['amount']         =	$row['amount'];
				$data[$i]['lastAmount']     =	$row['lastAmount'];
				$data[$i]['ordersid']       =	$row['ordersid'];
				$data[$i]['lastTime']       =	$row['lastTime'];
				$data[$i]['first_cate_use'] = $row['first_cate_use'];

				if(!$goodsid) {
					$totalId 				.=	$row['id'] . ',';
					$total_amount	+=	$row['amount'];
					$used_amount	+=	$row['lastAmount'];
					$unused_amount	=	$total_amount-$used_amount;	
				}
				$i++;
			}
			
			if(!$goodsid) {
				$totalId = rtrim($totalId,',');
				$sql = "SELECT b.`name`,a.`first_coupon_price`,a.`xia_dan_datetime` FROM `xzpf`.`goodsorders` as a INNER JOIN `xzpf`.`shop` as b ON (a.`shopid` = b.`shopid`) INNER JOIN `xzpf`.`orders_users_first` as c ON (c.`id` = a.`first_couponId`) where a.`first_couponId` IN (" . $totalId . ")";
				$sql = mysql_query($sql);

				while ($jilu = mysql_fetch_array($sql)) {
					$recording[$j]['name']					=	$jilu['name'];
					$recording[$j]['first_coupon_price']	=	$jilu['first_coupon_price'];
					$recording[$j]['xia_dan_datetime']		=	$jilu['xia_dan_datetime'];
					$j++;
				}

				$sql = "SELECT first_coupon FROM xitong";
				$rule = mysql_fetch_assoc(mysql_query($sql));
				return array($data, $recording, $total_amount, $used_amount, $unused_amount, $rule['first_coupon']);
			} else {
				// var_dump($data);die;
				return array('data'=>$data,'type'=>1);
			}

		} else {
			return false;
		}
	}

	/**
	 * [首单返券可使用金额]
	 * @Author   Axing
	 * @DateTime 2017-07-14
	 * @param    [string] 	$uid 			[商户id]
	 * @param    [json]		$goods_data 	[goodid:商品id,num:商品数量]
	 * @param    [string] 	$usersFirstId 	[首单返券id]
	 * @return   [float] 	$price 			[可使用金额]
	 */
	public function getPrice($uid, $goods_data, $usersFirstId) {
			$goods_data = json_decode($goods_data,true);
			$sql = "SELECT amount, lastAmount, first_cate_use, lastTime FROM orders_users_first where uid = " . $uid . " AND firstid = 1 AND id = " . $usersFirstId;
			$general  = mysql_fetch_assoc(mysql_query($sql));

			if($general != false) {
				foreach($goods_data as $key => $value) {
					$sql = "SELECT price FROM goods WHERE goodsid = " . $value['goodsid'];
					$row = mysql_fetch_assoc(mysql_query($sql));
					$price += $row['price']*$value['num'];

				}
				$price = $price*($general['first_cate_use']*0.01);
			} else {

					foreach($goods_data as $key => $value) {
						
						$sql = "SELECT  a.first_cate_use, c.price FROM orders_users_first AS a LEFT JOIN orders_first AS b ON b.id = a.firstid LEFT JOIN goods AS c ON c.xilie = b.cateid WHERE c.goodsid = " . $value['goodsid'] . " AND uid = " . $uid . " AND a.id = " . $usersFirstId;
						$row = mysql_fetch_assoc(mysql_query($sql));
						$price 	+=	$row['price']*$value['num'];
					}

					$price = $price*($row['first_cate_use']*0.01);
			}
			
			return $price;
	}

	/**
	 * [支付成功获取首单返券信息]
	 * @Author   Hybrid
	 * @DateTime 2017-08-14
	 * @return   [type]     [description]
	 */
/*	public function pay_fir_coupon($price, $user) {
		$sql = "SELECT * FROM orders_first";
		$res = mysql_query($sql);
		$i = 0;
		$data = array();
		while($row = mysql_fetch_array($res)) {
			$data[$i]['catename'] = $row['catename'];
			$data[$i]['first_price'] = ($row['first_cate_rate']*0.01)*1000;
			$i++;
		}
		return $data;
	}
*/

	/**
	 * [判断是否有返券]
	 * @Author   Hybrid
	 * @DateTime 2017-08-15
	 * @return   boolean    [description]
	 */
	public function is_fir_coupon($uid) {
		$sql = "SELECT a.*, b.`catename` FROM orders_users_first AS a LEFT JOIN orders_first AS b ON a.`firstid` = b.`id` WHERE uid = " . $uid;
		$res = mysql_query($sql);
		$i = 0;
		$data = array();
		while ($row = mysql_fetch_array($res)) {
			$data[$i]['catename']   = $row['catename'];
			$data[$i]['amount']     = $row['amount'];
			$data[$i]['lastAmount'] = $row['lastAmount'];
			$data[$i]['lastTime']   = $row['lastTime'];
			$i++;
		}
		return $data;
	}

	/**
	 * [首单返券总价]
	 * @Author   Hybrid
	 * @DateTime 2017-08-15
	 * @return   [type]     [description]
	 */
	public function fir_total($numbers, $userid) {
		$sql = "SELECT totalprice FROM goodsorders WHERE numbers = " . $numbers;
		$res = mysql_query($sql);
		$row = mysql_fetch_assoc($res);

		$sql = "SELECT COUNT(*) AS num FROM goodsorders WHERE state IN (1,2,3,4,5) AND userid = " . $userid;
		$num = mysql_fetch_assoc(mysql_query($sql));

		if($num == 1) {
			return $row['totalprice'];
		} else {
			return false;
		}
	}
}

$getFirCoupon = new getFirCoupon();
switch ($_GET['action']) {
	case 'getCoupon':
		$res = $getFirCoupon->getCoupon($_GET['uid'], $_GET['goodsid']);
		if ($res != false) {
			if($res['type'] == 1) {
				$arr = array('code' => 200, 'msg' =>'获取首单返券成功', 'data' => $res['data']);
			} else {
				$arr = array(
							'code'          => 200, 
							'msg'           => '获取首单返券成功', 
							'data'          => $res[0], 
							'recording'     => $res[1], 
							'total_amount'  => $res[2],
							'used_amount'   => $res[3],
							'unused_amount' => $res[4],
							'rule'          => $res[5]
							 );
			}
		} else {
			$arr =  array('code' => 100, 'msg' =>'插入数据失败');
		}
		break;
	
	case 'getPrice':
		$res = $getFirCoupon->getPrice($_GET['uid'], $_GET['goods_data'], $_GET['usersFirstId']);
		$arr = array('use_price' => $res);
		break;

	case 'pay_fir_coupon':
		$res = $getFirCoupon->pay_fir_coupon($_GET['price'], $_GET['user']);
		$arr = array('fir_price' => $res);
		break;

	case 'is_fir_coupon':
		$res = $getFirCoupon->is_fir_coupon($_GET['uid']);
		$arr = array('data' => $res);
		break;

	case 'fir_total':
		$res = $getFirCoupon->fir_total($_GET['numbers'], $_GET['userid']);
		$arr = array('totalprice' => $res);
		break;
}

echo json_encode($arr);
